import renderMap, { isCorrectPosition,reset } from "./render.js"
import playerMove from "./play.js"
import { box, allMap, correctNum } from "./map.js";
const GameOverTip = document.querySelector(".gameOver")
//上键ArrowUp 下键ArrowDown 左键ArrowLeft 右键ArrowRight
renderMap();
function init() {
    renderMap();
}
function addEvent() {
    document.onkeydown = (e) => {
        if (isSuccess()) {
            return;
        }
        switch (e.key) {
            case "ArrowUp":
                playerMove('U');
                renderMap();
                break;
            case "ArrowDown":
                playerMove('D');
                renderMap();
                break;
            case "ArrowLeft":
                playerMove('L');
                renderMap();
                break;
            case "ArrowRight":
                playerMove('R');
                renderMap();
                break;
        }
        if (isSuccess()) {
            GameoverEvent()
            console.log("成功")
        }
    }
}
//map.box的位置要和map.correctPosition的位置，遍历所有地图item，找出map.box的位置，如果每一个都相等则游戏完成
function isSuccess() {
    let isOver = 0;
    allMap.forEach((row, rowIndex) => {
        row.forEach((col, ColIndex) => {
            if (box === col) {
                isCorrectPosition(rowIndex, ColIndex) && (isOver += 1)
            }
        })
    })
    return isOver === correctNum;
}
function GameoverEvent() {
    //游戏结束后弹出窗口
    GameOverTip.classList.add("active");
    //点击开始下一局
    GameOverTip.onclick = function () {
        this.classList.remove("active");
        reset()
        renderMap()
    }
}

export default addEvent;